home *** CD-ROM | disk | FTP | other *** search
- Path: Rezonet.net!news
- From: ray@ultimate-tech.com (Ray Dunn)
- Newsgroups: comp.lang.c
- Subject: Re: Please help ?!
- Date: 31 Jan 1996 17:25:27 GMT
- Organization: Ultimate Technographics Inc.
- Message-ID: <4eo8m7$647@ns.RezoNet.NET>
- References: <4dm889$3hs@neptunus.pi.net> <4drnv1$cr@news.iag.net> <4drq5i$cr@news.iag.net> <TANMOY.96Jan28100725@qcd.lanl.gov>
- NNTP-Posting-Host: 204.19.230.7
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- In referenced article, Tanmoy Bhattacharya says...
- >This issue keeps coming up in this newsgroup, and as any stylistic
- >question, will probably never be `finally answered'. On compilers
- >which warn on undeclared functions, avoiding the cast has little
- >benefit in this particular case. One must keep in mind, however, that
- >programming is more a matter of habit: and, often, it is best not to
- >become too used to facilities one has at hand on a particular day on a
- >particular platform. The compiler _need not_ warn about an undeclared
- >function (i.e. such a warning is not mandated by ANSI: no warning is
- >however ever prohibited). Converting an int to a pointer without a
- >cast, however, needs a warning. On such compilers, then, avoiding the
- >cast can often help spot a fatal error.
-
- Perhaps I'm falling into the Trap that Tanmoy points out, i.e. relying
- on my tools to give warnings under these circumstances, but I feel very
- strongly that if your compiler does not point out these possible
- errors, then some version of lint should be run on code as part of a
- consistency check.
-
- Perhaps the use of such a facility belongs in Peter's recent rules for
- posting in c.l.c.
-
- >The point that Ray brings up is also one discussed here quite
- >often. The first thing is that writing
- >
- >long_ptr = malloc (sizeof (int));
- >
- >is inherently more error prone than writing
- >
- >long_ptr = malloc (sizeof*long_ptr);
- >
- >This second form is useful to learn as an idiom, and avoids most of
- >the errors that the explicit cast is trying to flag.
-
- I agree that this is the best solution.
-
- >A related point is that a `cast' is a loophole in type-safety, just as
- >a `goto' is a loophole in structured programming. Both have their
- >legitimate uses, and in the hands of an expert programmer, both of
- >them can integrate into magnificient products of art. A beginner
- >however is often advised to stay away from a goto, I wish the same
- >were done for casts. Unfortunately, C does not allow complete
- >elimination of casts: but avoiding them whenever possible is probably
- >an important lesson that every beginner needs to have.
-
- Unfortunately, many uses of "void *" are far bigger loopholes in type
- safety than a cast. Compilers or lint will pick up most erroneous uses
- of a cast. A wrong pointer type as the value of a void * function
- parameter can only be debugged at runtime - [a pox on MS for their use
- of general handle manipulating functions with void * parameters (:-(].
-
- --
- Ray Dunn (opinions are my own) | Phone: (514) 938 9050
- Montreal | Phax : (514) 938 5225
- ray@ultimate-tech.com | Home : (514) 630 3749
-
-